<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2018/11/27 0027
 * Time: 11:33
 */
namespace app\admin\model;
class NewBuilder
{
    private $type = 'table';
    private $title = '';
    private $feild = '';
    private $head = '';
    private $left = '';
    private $right = '';
    private $body = '';
    private $class='';
    private $style='';
    private $action = '';
    /**选择快速构建器的模板
     * @param $type
     * @return NewBuilder
     */
    static public function Make($type,$class=null,$style=null,$action=null){
        $obj = new NewBuilder();
        if($type){
            $obj->type = $type;
        }
        if($action){
            $obj->action = $action;
        }
        $obj->class = $class;
        $obj->style = $style;
        return $obj;
    }

    /**添加特定头信息
     * @return mixed
     */
    public function addTitle(){
            $str='<div class="my-btn-box">
    <span class="fl">
        <a class="layui-btn layui-btn-danger radius btn-delect" id="btn-delete-all">批量删除</a>
        <a class="layui-btn btn-add btn-default" id="btn-add" data-href="/admin/index/create_class.html">添加</a>
        <a class="layui-btn btn-add btn-default" id="btn-refresh"><i class="layui-icon">ဂ</i></a>
    </span>
    <span class="fr">
        <span class="layui-form-label">搜索条件：</span>
        <form action="/admin/index/classification.html" method="post">
        <div class="layui-input-inline">
            <input type="text" name="tiaojian" autocomplete="off" placeholder="请输入搜索条件" class="layui-input">
        </div>
        <button class="layui-btn mgl-20">查询</button>
        </form>
    </span>
</div>';
            $this->title = $str;
            return $this;
    }

    /**添加表格左栏
     * @param string描述 $text
     * @param null样式 $class
     * @param null样式 $style
     * @return $this
     */
    public function addLeft($text='',$class=null,$style=null){
        $this->left['class'] = $class;
        $this->left['text'] = $text;
        $this->left['style'] = $style;
        $this->left['html'] = '<td class="'.$class.'" style="'.$style.'"><input type="checkbox" id="quan">'.$text.'</td>';
        return $this;
    }

    /**添加表格列
     * @param 字段名 $name
     * @param 类型 $type
     * @param string描述 $desc
     * @param null样式 $class
     * @param null样式 $style
     * @return $this
     */
    public function addColumn($name,$type,$desc='',$class=null,$style=null){
        $this->feild[$name]['type'] = $type;
        $this->feild[$name]['class'] = $class;
        $this->feild[$name]['style'] = $style;
        $this->head .= "<td class='$class' style='$style'>$desc</td>";
        return $this;
    }

    /**添加表格右侧列
     * @param 标识 $text
     * @param array头信息数组 $title
     * @param array $action
     * @param array标识 $biaos
     * @param null样式 $class
     * @param null样式 $style
     * @return $this
     */
    public function addRight($text,$title=[],$action=[],$biaos=[],$class=null,$style=null){
        $this->right['title'] = $title;
        $this->right['action'] = $action;
        $this->right['biaos'] = $biaos;
        $this->right['class']=$class;
        $this->right['style'] = $style;
        $this->right['html'] = '<td class="'.$class.'" style="'.$style.'">'.$text.'</td>';
        return $this;
    }

    /**添加数据
     * @param $array
     * @return $this
     */
    public function addData($array){
//        echo '<pre>';print_r($array);exit;
        $body = '<tr>';
        $right = $this->right;
        foreach ($array as $value){
            if($this->left){
                $body.='<td class="'.$this->left['class'].'" style="'.$this->left['style'].'"><input type="checkbox" value="'.$value['id'].'"></td>';
            }
            if($this->feild){
                foreach ($this->feild as $k=>$v){
                    if($v['type']=='text'){
                        $body.='<td class="'.$v['class'].'" style="'.$v['style'].'">'.$value[$k].'</td>';
                    }elseif ($v['type']=='status'){
                        if($value[$k]){
                            $body.='<td class="'.$v['class'].'" style="'.$v['style'].'">启用</td>';
                        }else{
                            $body.='<td class="'.$v['class'].'" style="'.$v['style'].'">禁用</td>';
                        }
                    }
                }
            }
            if($right){
                $body.='<td class="'.$right['class'].'" style="'.$right['style'].'">';
                foreach ($right['title'] as $ke=>$va){
                    $body.='<a title="'.$right['title'][$ke].'" href="'.$right['action'][$ke].'"><i class="layui-icon">'.$right['biaos'][$ke].'</i></a>';
                }
                $body.='</td>';
            }
            $body.='</tr>';
        }
        $this->body = $body;
        return $this;
    }

    /**拼接所以字符串
     * @return string
     */
    public function getStr(){
        $str = '';
        if($this->type=='table'){
            if($this->title){
                $str.=$this->title;
            }
            $str.='<div class="layui-row"><table class="layui-table '.$this->class.'" style="'.$this->style.'" ><tr>';
            if($this->left){
                $str.=$this->left['html'];
            }
            $str.=$this->head;
            if($this->right){
                $str.=$this->right['html'];
            }
            $str.= $this->body;
            $str.='</table></div>';
            $str.='<div id="test"></div>';
        }elseif ($this->type=='form'){

        }
        return $str;
    }

    /**添加单行文本
     * @param 字段名name $name
     * @param string描述 $desc
     * @param string值 $value
     * @param null样式 $class
     * @param null样式 $style
     * @param bool是否禁用 $readonly
     */
    public function addInput($name,$desc='',$value='',$class=null,$style=null,$readonly=false){
        if($readonly){
            $str = '<div class="layui-form-item">
        <label class="layui-form-label">'.$desc.'</label>
        <div class="layui-input-block">
            <input type="text" name="'.$name.'" lay-verify="title" value="'.$value.'" autocomplete="off" placeholder="请输入标题" class="layui-input '.$class.'" style="'.$style.'" readonly="'.$readonly.'">
        </div>
    </div>';
        }else{
            $str = '<div class="layui-form-item">
        <label class="layui-form-label">'.$desc.'</label>
        <div class="layui-input-block">
            <input type="text" name="'.$name.'" lay-verify="title" value="'.$value.'" autocomplete="off" placeholder="请输入标题" class="layui-input '.$class.'" style="'.$style.'">
        </div>
    </div>';
        }
        $this->body .= $str;
        return $this;
    }

    /**添加隐藏域
     * @param 字段名name $name
     * @param string值 $value
     * @return $this
     */
    public function addNone($name,$value=''){
        $this->body .='<input type="text" name="'.$name.'" value="'.$value.'" style="display:none">';
        return $this;
    }

    /**添加多行文本
     * @param 字段名name $name
     * @param string描述 $desc
     * @param string值 $value
     * @param null样式 $class
     * @param null样式 $style
     * @return $this
     */
    public function addTextarea($name,$desc='',$value='',$class=null,$style=null){
        $this->body.='<div class="layui-form-item layui-form-text">
        <label class="layui-form-label">'.$desc.'</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入内容" name="'.$name.'" class="layui-textarea '.$class.'" style="'.$style.'">'.$value.'</textarea>
        </div>
    </div>';
        return $this;
    }

    /**添加下拉框
     * @param 字段名 $name
     * @param string描述 $desc
     * @param string值 $value
     * @param array选项 $option
     * @param null样式 $class
     * @param null样式 $style
     * @param null设置值和文字 $xuanz
     */
    public function addSelect($name,$desc='',$value='',$option=[],$class=null,$style=null,$xuanz=null){
        $this->body.='<div class="layui-form-item">
<label class="layui-form-label">'.$desc.'</label>
<select name="'.$name.'" class="layui-select '.$class.'" style="'.$style.'">';
    foreach ($option as $key=>$va){
        if($xuanz){
            if($value&&$value==$va[$xuanz['id']]){
                $this->body .='<option value="'.$va[$xuanz['id']].'" selected>'.$va[$xuanz['text']].'</option>';
            }else{
                $this->body .='<option value="'.$va[$xuanz['id']].'">'.$va[$xuanz['text']].'</option>';
            }
        }else{
            if($value&&$value==$va['id']){
                $this->body .='<option value="'.$va['id'].'" selected>'.$va['text'].'</option>';
            }else{
                $this->body .='<option value="'.$va['id'].'">'.$va['text'].'</option>';
            }
        }
    }
$this->body.='</select>
</div>';
        return $this;
    }

    /**添加图片
     * @param 字段名 $name
     * @param string描述 $desc
     * @param null值 $value
     * @param null样 $class
     * @param null样式 $style
     * @param bool是否多选 $iss
     */
    public function addImg($name,$desc='',$value=null,$class=null,$style=null,$iss=false){
        $this->body.='<div class="layui-form-item">
        <label class="layui-form-label">'.$desc.'</label>
        <div class="layui-input-block">';
        if($iss){
            $this->body.='<input type="file" name="'.$name.'[]" class="'.$class.'" onchange="yulan(this)" style="'.$style.'" multiple>';
        }else{
            $this->body.='<input type="file" name="'.$name.'" class="'.$class.'" onchange="yulan(this)" style="'.$style.'">';
        }

        $this->body.='</div>
    </div><div class="layui-form-item">
        <label class="layui-form-label">'.$desc.'预览</label>
        <div class="layui-input-block">';
            if(is_array($value)){
                foreach ($value as $va){
                    $this->body.='<img src="'.$va.'" width="100" style="margin-right: 5px">';
                }
            }else{
                $this->body.='<img src="'.$value.'" width="100">';
            }
        $this->body.='</div>
    </div>';
            return $this;
    }

    /**添加文件域
     * @param 字段名 $name
     * @param string描述 $desc
     * @param string值 $value
     * @param null样式 $class
     * @param null样式 $style
     * @param bool是否多文件 $iss
     */
    public function addFile($name,$desc='',$value='',$class=null,$style=null,$iss=false){
        $this->body.='<div class="layui-form-item">
        <label class="layui-form-label">'.$desc.'</label>
        <div class="layui-input-block">';
        if($iss){
            if(is_array($value)){
                $value = implode(',',$value);
            }
            $this->body.='<input type="file" name="'.$name.'[]" class="'.$class.'" value="'.$value.'" style="'.$style.'" multiple>';
        }else{
            $this->body.='<input type="file" name="'.$name.'" class="'.$class.'" value="'.$value.'" style="'.$style.'">';
        }
        $this->body.='</div>
    </div>';
    }

    /**加单选框
     * @param 字段名$name
     * @param string描述 $desc
     * @param string值 $value
     * @param array选项 $option
     * @param null样式 $class
     * @param null样式 $style
     * @return $this
     */
    public function addRadio($name,$desc='',$value='',$option=[],$class=null,$style=null){
        $this->body.='<div class="layui-form-item">
                <label class="layui-form-label">'.$desc.'</label>
                <div class="layui-input-block">';
        foreach ($option as $va){
            if($va['id']==$value){
                $this->body.='<input type="radio" name="'.$name.'" class="'.$class.'" style="'.$style.'" value="'.$va['id'].'" title="'.$va['text'].'" checked>';
            }else{
                $this->body.='<input type="radio" name="'.$name.'" value="'.$va['id'].'" class="'.$class.'" style="'.$style.'"  title="'.$va['text'].'" >';
            }
        }
$this->body.='</div>
        </div>';
        return $this;
    }

    /**获取多选框
     * @param 字段名称 $name
     * @param string描述 $desc
     * @param array值 $value
     * @param array选项 $option
     * @param null样式 $class
     * @param null样式 $style
     * @return $this
     */
    public function addCheckbox($name,$desc='',$value=[],$option=[],$class=null,$style=null){
        $this->body.='<div class="layui-form-item" pane="">
        <label class="layui-form-label">'.$desc.'</label>
        <div class="layui-input-block">';
        foreach ($option as $va){
            if(in_array($va['id'],$value)){
                $this->body.='<input type="checkbox" name="'.$name.'[]" class="'.$class.'" style="'.$style.'" lay-skin="primary" title="'.$va['text'].'" checked="">';
            }else{
                $this->body.='<input type="checkbox" name="'.$name.'[]" class="'.$class.'" style="'.$style.'" lay-skin="primary" title="'.$va['text'].'">';
            }
        }
        $this->body.='</div>
    </div>';
        return $this;
    }

    /**加载编辑框
     * @param 字段名 $name
     * @param string描述 $desc
     * @param string值 $value
     * @param null样式 $class
     * @param null样式 $style
     * @return $this
     */
    public function addEditor($name,$desc='',$value='',$class=null,$style=null){
        $this->body.='<div class="layui-form-item layui-form-text">
        <label class="layui-form-label">'.$desc.'</label>
        <div class="layui-input-block">
            <textarea class="layui-textarea layui-hide" class="'.$class.'" style="'.$style.'"  name="'.$name.'" lay-verify="content" id="'.$name.'">'.$value.'</textarea>
        </div>
    </div><script>
    var '.$name.'=layui.layedit.build("'.$name.'");
</script>';
        return $this;
    }
}